package com.cdfahe.gms.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ModelAttribute;

import com.cdfahe.common.Constants;
import com.cdfahe.common.util.page.ResultFilter;
import com.cdfahe.gms.mybatis.entity.News;
import com.cdfahe.gms.mybatis.entity.NewsExample;
import com.cdfahe.gms.mybatis.mapper.NewsMapper;
import com.cdfahe.gms.service.INewsService;
import com.sun.org.apache.regexp.internal.recompile;
@Service
public class NewsServiceImpl implements INewsService {
	@Resource
	private NewsMapper newsMapper;
	
	public ResultFilter<News> listNews(ResultFilter<News> rf,int type,int pagesize){ 
		rf.setPageSize(pagesize);
	    rf.setTotalCount(getTotalNewByType(type));  
	    rf.setItems(selectNewsByPageAndType(type, (rf.getCurrentPage() - 1 )* rf.getPageSize(), rf.getPageSize()));
		return rf;  
	} 
	public int getTotalNewByType(int type){
		NewsExample example = new NewsExample();
		example.createCriteria().andCategoryidEqualTo(type)
		.andNewstypeEqualTo(Constants.NEWS_TYPE_STATIC);
		return newsMapper.countByExample(example);
	}
	@Override
	public void delNewsById(int idnum) {
		// TODO Auto-generated method stub

	}

	@Override
	public News getNewsById(int idnum) {
		return newsMapper.selectByPrimaryKey(idnum);
	}

	@Override
	public void updateNewsByExample(NewsExample example) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateNewsById(News u) {
		newsMapper.updateByPrimaryKeySelective(u);
	}

	@Override
	public List<News> selectNewsByExample(NewsExample example) {
		return newsMapper.selectByExample(example);
	}

	@Override
	public List<News> selectNewsByPageAndType(int type, int page,int pagesize) {
		return newsMapper.selectNewsByPageAndType(type, page, pagesize);
	}
	
//	@Override
//	public List<News> selectNewsByPageAndType(int type, int page,int pagesize) {
//		return newsMapper.selectNewsByPageAndType(type, (page - 1)*pagesize, pagesize);
//	}
	
	@Override
	public void addNews(News news) {
		newsMapper.insert(news);
	}

}
